-- -- 定义一个函数，这个函数会在玩家进入世界时执行
-- local function screenAH()
--     AHscreenData = screenAuctionator("厄运之槌 Alliance", "匕首岭 Alliance")
--     print("转服利润计算完毕，共" .. #AHscreenData)
-- end

-- -- 监听PLAYER_ENTERING_WORLD事件
-- -- 使用RegisterEvent来注册你希望监听的事件
-- -- 使用FrameXML中的某个框架来注册事件，这里使用UI框架
-- local frame = CreateFrame("Frame", "AHscreen", UIParent)
-- frame:SetSize(400, 200)
-- frame:SetPoint("CENTER", UIParent, "CENTER");
-- frame:EnableMouse(true);
-- frame:SetMovable(true)
-- frame:SetFrameStrata("HIGH")

-- frame:RegisterEvent("PLAYER_ENTERING_WORLD")
-- local btn1 = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
-- btn1:SetText("AH扫描")
-- btn1:SetSize(40, 20)
-- btn1:SetPoint("TOPLEFT", frame, "TOPRIGHT", 100, 0)
-- btn1:SetScript("OnClick", screenAH)
-- btn1:Show()

-- frame:Show()
-- -- 定义事件的处理函数
-- frame:SetScript("OnEvent", function(self, event, ...)
--     if event == "PLAYER_ENTERING_WORLD" then
--         print("AHscreenData加载完毕...")
--         screenAH()
--         screenAH()
--     end
-- end)


AHscreenData = {}

local function screenAuctionator(fromStr, toStr) --遍历auctionator中的数据
    local AHlist = {}
    if AUCTIONATOR_PRICE_DATABASE then
        local from = AUCTIONATOR_PRICE_DATABASE[fromStr]
        local to = AUCTIONATOR_PRICE_DATABASE[toStr]
        if from and to then
            for key, value in pairs(from) do
                if string.find(key, "g") then
                    --如果是装备，不处理
                else
                    --先计算差价
                    local fromPrice = value["m"]
                    local toPrice = -1
                    if to[key] then
                        toPrice = to[key]["m"]
                    end
                    if fromPrice > 0 and toPrice > 0 then -- 如果两个服务器都有价格
                        local profit = toPrice * 0.95 - fromPrice
                        if profit > 100 then              -- 如果利润大于1金
                            local fitRate = profit / fromPrice
                            --获取物品名称
                            local item = { GetItemInfo(key) }
                            if item[12] == 2 or item[12] == 4 then
                                if item[5] == 80 then --装备则判断是否是80级的且为紫装
                                    AHlist[#AHlist + 1] = { key, "装备" .. item[3] .. ":" .. item[1], fromStr ..
                                    ":" .. fromPrice, toStr .. ":" ..
                                    toPrice, "跨服利润:" .. profit,
                                        math.floor(fitRate) } -- id ,name ,price
                                end
                            else
                                AHlist[#AHlist + 1] = { key, item[1], fromStr .. ":" .. fromPrice, toStr .. ":" ..
                                toPrice, "跨服利润:" .. profit,
                                    math.floor(fitRate) } -- id ,name ,price
                            end
                        end
                    end
                end
            end
        end
    else
        print("数据库变量不存在....")
    end
    if #AHlist then --根据fitRate排序
        table.sort(AHlist, function(a, b)
            return a[6] > b[6]
        end)
    end
    return AHlist
end
local function screenAH()
    AHscreenData = screenAuctionator("厄运之槌 Alliance", "匕首岭 Alliance")
    print("转服利润计算完毕，共" .. #AHscreenData)
end

local frame = CreateFrame('Frame', 'frameName', UIParent)
frame:SetSize(200, 120);
frame:SetPoint("CENTER", UIParent, "CENTER");

-- 创建按钮并设置其属性
local button = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate");
button:SetSize(100, 20); -- 设置按钮大小
button:SetPoint("TOPLEFT", frame, "CENTER", 0, 0);
button:SetText("跨服利润计算");
button:SetScript("OnClick", screenAH);
local button2 = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate");
button2:SetSize(100, 20); -- 设置按钮大小
button2:SetPoint("TOPLEFT", frame, "CENTER", 0, 0);
button2:SetText("跨服利润计算");
button2:SetScript("OnClick", screenAH);

-- -- 注册事件
-- frame:RegisterEvent("ADDON_LOADED");
-- frame:SetScript("OnEvent", function(self, event, ...)
--     if event == "ADDON_LOADED" and select(1, ...) == myAddonName then
--         self:OnInitialize();
--         self:UnregisterEvent("ADDON_LOADED");
--         self:OnEnable();
--     end
-- end);
